<template>
  <div class="approvals-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <tree-tools :node-data="company" :is-root="true" @add-depts="handleAddDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- node,data是插槽本身带的 -->
          <template #default="{data}">
            <!-- {{ data.name }} -->
            <tree-tools
              :node-data="data"
              @del-depts="GetDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDeptDialog"
      :show-dialog.sync="showDialog"
      :node-data="nodeData"
      :depts="depts"
      @add-depts="GetDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      loading: false,
      depts: [],
      nodeData: {},
      showDialog: false,
      company: {},
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.GetDepartments()
  },
  methods: {
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$refs.addDeptDialog.getEmployeeSimple()
      this.$refs.addDeptDialog.getDepartDetail(nodeData.id)
    },
    handleAddDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    closeDialogFn(flag) {
      this.showDialog = flag
    },
    async GetDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()
      // console.log(data)
      this.company = {
        name: companyName,
        manager: '负责人',
        id: ''
      }
      this.depts = depts
      this.departs = tranListToTreeData(depts, '')
      // console.log(this.departs)
      this.loading = false
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
</style>
